﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum Transition
{
    NullTransition =0,
    IsWalk = 1,
    IsRun = 2,
    IsPush = 3,
    IsClimb = 4,
    IsPick = 5,
    IsPreAttack = 6,
    IsAttack = 7,
    IsDoubleAttack = 8,
    IsBeAttacked = 9,
    IsHide = 10,
    IsDie = 11,
    isIdle = 12,
}
public enum StateID
{
    NullStateID = 0,
    WalkID =1,
    RunID = 2,
    PushID=3,
    ClimbID=4,
    PickID=5,
    PreAttackID=6,
    AttackID=7,
    DoubleAttackID=8,
    BeAttackedID=9,
    HideID=10,
    DieID=11,
    IdleID=12,
}


public abstract class IPlayerState
{
    public Animator animator = GameObject.FindGameObjectWithTag("Player").GetComponent<Animator>();
    protected Dictionary<Transition, StateID> playerFSMDict = new Dictionary<Transition, StateID>();
    protected StateID stateID;
    public StateID ID { get { return stateID; } }

    public void AddTransition(Transition trans, StateID id)
    {
        // Check if anyone of the args is invalid
        if (trans == Transition.NullTransition)
        {
            Debug.LogError("FSMState ERROR: NullTransition is not allowed for a real transition");
            return;
        }

        if (id == StateID.NullStateID)
        {
            Debug.LogError("FSMState ERROR: NullStateID is not allowed for a real ID");
            return;
        }

        if (playerFSMDict.ContainsKey(trans))
        {
            Debug.LogError("FSMState ERROR: State " + stateID.ToString() + " already has transition " + trans.ToString() +
                           "Impossible to assign to another state");
            return;
        }
        playerFSMDict.Add(trans, id);
    }

    public void DeleteTransition(Transition trans)
    {
        // Check for NullTransition
        if (trans == Transition.NullTransition)
        {
            Debug.LogError("FSMState ERROR: NullTransition is not allowed");
            return;
        }
        // Check if the pair is inside the map before deleting
        if (playerFSMDict.ContainsKey(trans))
        {
            playerFSMDict.Remove(trans);
            return;
        }
        Debug.LogError("FSMState ERROR: Transition " + trans.ToString() + " passed to " + stateID.ToString() +
                       " was not on the state's transition list");
    }

    public StateID GetOutputState(Transition trans)
    {
        if (playerFSMDict.ContainsKey(trans))
        {
            return playerFSMDict[trans];
        }
        return StateID.NullStateID;
    }

    //状态切换前调用
    public virtual void DoBeforeEntering() { }
    //状态切换后调用
    public virtual void DoBeforeLeaving() { }
    //每帧调用的，判断是否需要切换状态
    public abstract void Reason(GameObject player);
    //每帧调用的，游戏逻辑
    public abstract void Act(GameObject player);
}
